package com.example.mallDemo.controller;

//功能：前后台数据交互类
//日期：2022年10月31日

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mallDemo.common.Result;
import com.example.mallDemo.entity.Goods;
import com.example.mallDemo.mapper.GoodsMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController     //定义 说明是一个返回js数据的controller
@RequestMapping("/goods")     //定义接口统一路由
public class GoodsController {
    @Resource          //引入mapper（不太规范）
            GoodsMapper goodsMapper;


    //    数据库，插入数据
    @PostMapping     //定义post接口，接收前台数据
    public Result<?> save(@RequestBody Goods goods){            //@RequestBody  将传过来的js转换为java对象
        goodsMapper.insert(goods);    //insert方法，将数插入数据库
        return Result.success();
    }
    //    删除数据接口类
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id){            //@RequestBody  将传过来的js转换为java对象
        goodsMapper.deleteById(id);
        return Result.success();
    }
    //数据库更新类
    @PutMapping      //定义post接口，接收前台数据   更改数据接口
    public Result<?> update(@RequestBody Goods goods){            //@RequestBody  将传过来的js转换为java对象
        goodsMapper.updateById(goods);    //updateByID方法，更新数据库
        return Result.success();
    }
    //查询数据库
    @GetMapping     //后台分页查询
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "20") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Goods::getName,search);
        }
        Page<Goods>goodsPage =goodsMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(goodsPage);
    }
    @GetMapping("/userGoods")     //后台分页查询
    public Result<?> findPage2(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "20") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Goods::getAuthor,search);
        }
        Page<Goods>goodsPage =goodsMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(goodsPage);
    }

    @GetMapping("/fwqClazz")     //后台分页查询
    public Result<?> findPage3(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "20") Integer pageSize,
                               @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Goods::getFwq,search);
        }
        Page<Goods>goodsPage =goodsMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(goodsPage);
    }
}
